import time
from collections import OrderedDict
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from timm.models import inception_resnet_v2
BatchNorm2d = nn.BatchNorm2d

class PadTop(nn.Module):
    def __init__(self):
        super(PadTop, self).__init__()
    def forward(self, xs):
        xs = torch.cat([xs[:, :, 0:1, :], xs], dim=2)
        return xs


class ConvNormAct(nn.Module):
    def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=(0, 0), dilation=(1, 1), bias=False):
        super(ConvNormAct, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, padding=padding, stride=stride,
                      bias=bias,dilation=dilation),
            BatchNorm2d(out_channels, momentum=0.1),
            nn.ReLU(inplace=True)
        )
    def forward(self, x):
        x = self.conv(x)
        return x
class Mixed_Down(nn.Module):
    def __init__(self, in_ch):
        super(Mixed_Down, self).__init__()
        self.branch0 = ConvNormAct(in_ch, in_ch, kernel_size=3, stride=2, padding=1)
        self.branch1 = nn.Sequential(
            ConvNormAct(in_ch, in_ch//2, kernel_size=1, stride=1),
            ConvNormAct(in_ch//2, in_ch//2, kernel_size=3, stride=1, padding=1),
            ConvNormAct(in_ch//2, in_ch//2, kernel_size=3, stride=2, padding=1)
        )
        self.branch2 = nn.Sequential(
            ConvNormAct(in_ch, in_ch//2, kernel_size=1, stride=1),
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        )
    def forward(self, x):
        x0 = self.branch0(x)
        x1 = self.branch1(x)
        x2 = self.branch2(x)
        out = torch.cat((x0, x1, x2), 1)
        return out
class NormDown(nn.Module):
    def __init__(self, in_ch):
        super(NormDown, self).__init__()
        self.branch0 = ConvNormAct(in_ch, in_ch*2, kernel_size=3, stride=2, padding=1)

    def forward(self, x):
        x0 = self.branch0(x)
        return x0



class R2DliaBlock(nn.Module):
    expansion = 1
    def __init__(self, inplanes, planes, scale_factor=1):
        super(R2DliaBlock, self).__init__()
        self.relu = nn.ReLU(inplace=True)
        self.width = inplanes*scale_factor
        self.ip = self.width//4
        self.conv0 = ConvNormAct(inplanes, self.width, kernel_size=3, stride=1, padding=1)
        self.conv1 = ConvNormAct(self.ip, self.ip, kernel_size=1, padding=0)
        self.conv2 = ConvNormAct(self.ip, self.ip, kernel_size=3, padding=1)
        self.conv3 = ConvNormAct(self.ip, self.ip, kernel_size=3, padding=2, dilation=2)
        self.conv4 = ConvNormAct(self.ip, self.ip, kernel_size=3, padding=3, dilation=3)
        self.conv_out = nn.Sequential(
            nn.Conv2d(self.width, planes, kernel_size=1, stride=1, padding=0, bias=False),
            BatchNorm2d(planes),
        )
    def forward(self, x):
        residual = x
        out = self.conv0(x)
        x1, x2, x3, x4 = torch.split(out, self.ip, dim=1)
        x1 = self.conv1(x1)
        x2 = self.conv2(x1+x2)
        x3 = self.conv3(x2+x3)
        x4 = self.conv4(x3+x4)
        out = torch.cat([x1, x2, x3, x4], dim=1)
        out = self.conv_out(out)
        out += residual
        out = self.relu(out)
        return out
class CovBlock(nn.Module):
    expansion = 1
    def __init__(self, inplanes):
        super(CovBlock, self).__init__()
        self.conv1 = ConvNormAct(inplanes, inplanes, kernel_size=3, stride=1, padding=1)
        self.conv2 = nn.Conv2d(inplanes, inplanes, kernel_size=3, stride=1, padding=1, bias=False)
        self.bn2 = BatchNorm2d(inplanes , momentum=0.1)
        self.relu = nn.ReLU(inplace=True)
    def forward(self, x):
        residual = x
        out = self.conv1(x)
        out = self.conv2(out)
        out = self.bn2(out)
        out += residual
        out = self.relu(out)
        return out


class DslNet(nn.Module):
    def __init__(self, is_pad=True):
        self.inplanes = 64
        super(DslNet, self).__init__()
        self.steam = ConvNormAct(3, 64, kernel_size=3, stride=2, padding=1)
        self.conv1 = nn.Sequential(
            #nn.ZeroPad2d(padding=(0, 0, 1, 1)),
            ConvNormAct(64, 64, kernel_size=3, stride=2, padding=1),
            CovBlock(64),
            CovBlock(64)
        )
        self.down1 = NormDown(64)
        self.conv2 = nn.Sequential(
            R2DliaBlock(128,128),
            R2DliaBlock(128,128)
        )
        self.down2 = NormDown(128)
        self.conv3 = nn.Sequential(
            R2DliaBlock(256, 256,scale_factor=1),
            R2DliaBlock(256, 256,scale_factor=1),
            R2DliaBlock(256, 256,scale_factor=1),
            R2DliaBlock(256, 256,scale_factor=1),

        )
        self.down3 = NormDown(256)
        self.conv4 = nn.Sequential(
            R2DliaBlock(512, 512, scale_factor=1),
            R2DliaBlock(512, 512, scale_factor=1),
        )

    def forward(self, x):
        x1 = self.steam(x)
        x1 = self.conv1(x1)

        x2 = self.down1(x1)
        x2 = self.conv2(x2)

        x3 = self.down2(x2)
        x3 = self.conv3(x3)

        x4 = self.down3(x3)
        x4 = self.conv4(x4)

        return x1,x2,x3,x4





def resnet_xag_18(is_pad=True):
    model = DslNet(is_pad)
    return model



if __name__ == '__main__':
    md = resnet_xag_18()

    x = torch.randn(1,3, 540 ,960)
    for i in range(1000000):
        t = time.time()
        md(x)
        print(1/(time.time()-t))
